package com.hll.servlet;

import com.hll.pojo.User;
import com.hll.thread.SendMailThread;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: Mr.Hu
 * @create: 2021-03-09 09:52
 */
public class Register extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String username = req.getParameter("name");
    String password = req.getParameter("pwd");
    String email = req.getParameter("mail");

    // 主线程main和子线程不干扰
    // 2、向用户邮箱发送邮件，注意发送邮件很耗时，所以我们启动一个子线程去做这件事，而用户则是直接跳转注册成功页面，以免降低用户体验
    User user = new User(username, password, email);
    SendMailThread sendmail = new SendMailThread(user); // 获取子线程对象
    new Thread(sendmail).start(); // 启动子线程

    // 3、视图跳转
    req.setAttribute("message", "注册成功！我们已经向您的邮箱发送了邮件，请您及时进行查收。由于网络原因，您收到邮件的时间存在延迟，敬请谅解~");
    req.getRequestDispatcher("info.jsp").forward(req, resp);
  }
}
